<template>
	<view class="classList">
		<view class="loadingLayout" v-if="!wallList.length && !noData">
			<uni-load-more status="loading"></uni-load-more>
		</view>
		
		<view class="content">
			<navigator :url="'/pages/preview/preview?id=' + item._id" class="item" v-for="item in wallList" :key="item._id">
				<image :src="item.smallPicurl" mode="aspectFill"></image>
			</navigator>
		</view>
		
		<view class="loadingLayout" v-if="wallList.length || noData">
			<uni-load-more :status=" noData?'noMore':'loading'"></uni-load-more>
		</view>
		
		<view class="safe-area-inset-bottom"></view>
	</view>
</template>

<script setup>
import { ref } from 'vue';
import {onLoad, onUnload, onPullDownRefresh, onReachBottom, onShareAppMessage, onShareTimeline} from '@dcloudio/uni-app'
import {apiGetWall, apiUserWallList} from '@/apis/apis.js'


const wallList = ref([])
const queryParams = {
	pageNum:1,
	pageSize:12
}
const noData = ref(false)



//下拉刷新
onPullDownRefresh(()=>{
	queryParams.pageNum = 1
	wallList.value = []
	getWallList()
	uni.stopPullDownRefresh()
})

//触底加载
onReachBottom(()=>{
	if(noData.value) return
	queryParams.pageNum ++
	getWallList()
})


let  pageName;
//获取上一个页面传过来的id,name值
onLoad((e)=>{
	// console.log(e)
	
	let {id=null,name=null,type=null} = e;
	if(type) queryParams.type = type
	if(id) queryParams.classid = id;
	pageName = name
	//修改导航标题
	// console.log(name);
	uni.setNavigationBarTitle({
		title:name
	})
	
	//执行获取分类列表方法
	getWallList()
})

//获取分类列表网络数据
const getWallList = async ()=>{
	let res
	if(queryParams.classid) res = await apiGetWall(queryParams)
	if(queryParams.type) res = await apiUserWallList(queryParams)
	
	// console.log(res)
	wallList.value = [...wallList.value, ...res.data]
	
	//设置缓存
	uni.setStorageSync("storeWallList",wallList.value)
	
	if(queryParams.pageSize > res.data.length) noData.value = true
}


//分享给朋友
onShareAppMessage(()=>{
	return {
		title:"卷毛壁纸-"+pageName,
		path:"/pages/classlist/classlist?id=" + queryParams.classid + "&name=" + pageName ,
		imageUrl:"/static/images/logo.png"
	}
})

//分享朋友圈
onShareTimeline(()=>{
	return {
		title:"卷毛壁纸-"+pageName,
		query:"id="+queryParams.classid+"&name="+pageName
	}
})

onUnload(()=>{
	uni.removeStorageSync("storeWallList")
})


</script>

<style lang="scss" scoped>
.classList{
	.loadingLayout{
		padding: 30rpx 0;
	}
	.content{
		display: grid;
		grid-template-columns: repeat(3,1fr);
		gap: 5rpx;
		padding: 5rpx;
		.loadingLayout{
			display: flex;
			justify-content: center;
		}
		.item{
			height: 440rpx;
			image{
				width: 100%;
				height: 100%;
				display: block;
			}
		}
	}
}

</style>
